package com.hopes.bizz.risk.control.evaluate;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hopes.bizz.risk.common.constant.HidDicConstant;
import com.hopes.bizz.risk.common.constant.enums.RiskHidSourceEnum;
import com.hopes.bizz.risk.control.bo.NegatedItem;
import com.hopes.bizz.risk.control.bo.RiskEffectEvaluationScore;
import com.hopes.bizz.risk.control.bo.RiskEvaluateBo;
import com.hopes.bizz.risk.hid.entity.RiskHidHiddenDangerEntity;
import com.hopes.bizz.risk.hid.service.RiskHidHiddenDangerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lyb
 * @version 1.0
 * @description: 本周有重大隐患超期未整改
 * @date 2023/7/6 21:14
 */
@Component
@Slf4j
@Order(1005)
public class RiskHidRectifiedRejectFactorHandler extends RiskRejectFactorHandler {

    @Resource
    private RiskHidHiddenDangerService hidHiddenDangerService;

    @Override
    protected NegatedItem doCalculate(RiskEvaluateBo riskEvaluateBo, NegatedItem negatedItem) {
        LambdaQueryWrapper<RiskHidHiddenDangerEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RiskHidHiddenDangerEntity::getBizCode, RiskHidSourceEnum.RISK_CONTROL.getCode());
        wrapper.eq(RiskHidHiddenDangerEntity::getHidLevel, HidDicConstant.DANGER_LEVEL_MAJOR);
        wrapper.eq(RiskHidHiddenDangerEntity::getHidStatus, HidDicConstant.HID_DANGER_STATUS_WAIT_CONFIRM + "," + HidDicConstant.HID_DANGER_WAIT_RECTIFICATION);
        wrapper.ge(RiskHidHiddenDangerEntity::getReportDate, riskEvaluateBo.getStartTime());
        wrapper.le(RiskHidHiddenDangerEntity::getReportDate, riskEvaluateBo.getEndTime());
        List<RiskHidHiddenDangerEntity> hidHiddenDangers = hidHiddenDangerService.list(wrapper);
        if (CollectionUtil.isNotEmpty(hidHiddenDangers)) {
            log.info("本周存在重大隐患");
            String hidName = hidHiddenDangers.stream().map(RiskHidHiddenDangerEntity::getHidName).collect(Collectors.joining("、"));
            negatedItem.setNegatedItemFive(Boolean.FALSE);
            negatedItem.setNegatedItemFiveReason("本周存在重大隐患未整改,隐患名称: " + hidName);
        } else {
            negatedItem.setNegatedItemFiveReason("本周未上报重大隐患");
        }
        return doNextCalculate(riskEvaluateBo, negatedItem);
    }

    @Override
    protected Boolean isUse(RiskEffectEvaluationScore riskEffectEvaluationScore) {
        return riskEffectEvaluationScore.getHidExceedTimeFlag();
    }

}
